package com.boxuegu.dao;
import com.boxuegu.controller.LineController;
import com.boxuegu.service.LineService;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;


import java.io.BufferedReader;
import java.util.Arrays;

public class LineDao {
    //定义存储所有站点信息的集合


    /**
     * 库管读取库存中所有路线信息, 并将信息添加到集合, 返回给业务员
     */
    public ArrayList<Object> findAllRoutes() {

        ArrayList<Object> allRoutesList = new ArrayList();
        //将读取到的站点信息添加到allRoutesList集合
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("F:\\bm\\beijingmetro\\src\\subway.txt"));

            String str = null;
            while ((str = br.readLine()) != null) {
                allRoutesList.add(str);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

        return allRoutesList;
    }

    public static String viewStation() throws IOException {
        String str1 = new String();
        //从文件读取
        FileReader fr = new FileReader("F:\\bm\\beijingmetro\\src\\user.txt");

        char[] chars = new char[1024];
        int len;

        while ((len = fr.read(chars)) != -1) {
            String str = new String(chars, 0, len);
            str1 = str;
        }
        fr.close();
        //返回一个字符串
        return str1;
    }

    //将字符串修改为字符串数组
    public static String[] arrayString() throws IOException {
        String str = viewStation();
        //将接收到字符串转换为字符串数组,按回车切割
        String[] arrStr = str.split("\r\n");
        //上步切割得到的数组的每一索引里的字符串再进行切割并存储起来
        String[] ss1 = arrStr[0].split(" --> ");
        String[] ss2 = arrStr[1].split(" --> ");
        //分别将这两个字符串数组的第一个去掉1号线和4号线
        ss1[0] = "苹果园";
        ss2[0] = "安河桥北";

        // 保存第一个数组长度
        int strlen1 = ss1.length;
        // 保存第二个数组长度
        int strlen2 = ss2.length;
        // 扩容
        ss1 = Arrays.copyOf(ss1, strlen1 + strlen2);
        // 将第二个数组与第一个数组合并
        System.arraycopy(ss2, 0, ss1, strlen1, strlen2);
        return ss1;
    }

    //输入一个站点名，返回其在合并后的字符串数组的索引位置
    public static int index(String station) throws IOException {
        String[] str = arrayString();
        int index = 0;
        for (int i = 0; i < str.length; i++) {
            if (station.equals(str[i])) {
                index = i;
            }

        }
        return index;
    }

    //线路提示信息
    public static void lineShow(int index) {
        if (index == 0) {
            System.out.print("1号线:");
        }
        if (index == 1) {
            System.out.print("4号线:");
        }
    }

    //两条线路
    public static int line2(String start, String end) throws IOException {
        //定义二维数组
        String[][] str = new String[2][];

        //idex1，index11 记录start索引位置
        int idex1 = -1, idex11 = -1;
        //idex2，index22 记录end索引位置
        int idex2 = -1, idex22 = -1;
        //m,n分别记录start和end在它们各自所在行的位置
        int m = -1, n = -1;
        //记录经过站的数目
        int count = 0;

        String[] s = arrayString();
        //获得4号线的起始点的索引
        int idex = index("安河桥北");
        //将一维数组以4号线起始点为分界线分成二维数组
        str[0] = new String[idex];
        str[1] = new String[s.length - idex];

        System.arraycopy(s, 0, str[0], 0, idex);
        System.arraycopy(s, idex, str[1], 0, s.length - idex);


        //双重循环遍历得到起始站和终点站的索引位置
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < str[i].length; j++) {
                if (start.equals(str[i][j])) {
                    idex1 = j;
                    idex11 = i;
                }

                if (end.equals(str[i][j])) {
                    idex2 = j;
                    idex22 = i;
                }
            }
        }
        //判断是否在同一条线路
        if (idex11 == idex22) {
            lineShow(idex22);
            //在同一条线路
            if (idex1 <= idex2) {
                //start在前 end在后
                for (int i = idex1; i < idex2; i++) {
                    System.out.print(str[idex11][i] + "->");
                    count++;
                }

            }
            if (idex1 > idex2) {
                //start在后 end在前
                for (int i = idex1; i > idex2; i--) {
                    System.out.print(str[idex11][i] + "->");
                    count++;
                }

            }
        } else {
            //不在同一条线路
            for (int i = 0; i < str[idex11].length; i++) {
                for (int j = 0; j < str[idex22].length; j++) {
                    if (str[idex11][i].equals(str[idex22][j])) {
                        //记录相同站点的位置
                        m = i;
                        n = j;
                    }
                }
            }
            if (m == -1) {
                System.out.println("输入终点站信息有误，本站无法到达您的终点站");
                LineService.routeMap();
                System.exit(0);
            }
            //start在换乘站的前面
            if (idex1 < m) {
                //end在换乘站的前面
                if (idex2 < n) {
                    lineShow(idex11);
                    for (int i = idex1; i < m; i++) {
                        System.out.print(str[idex11][i] + "->");
                        count++;
                    }
                    System.out.println();
                    System.out.print("在此换乘");
                    lineShow(idex22);
                    for (int j = n; j > idex2; j--) {
                        System.out.print(str[idex22][j] + "->");
                        count++;
                    }

                } else {
                    //end在换乘站后面
                    lineShow(idex11);
                    for (int i = idex1; i < m; i++) {
                        System.out.print(str[idex11][i] + "->");
                        count++;
                    }
                    System.out.println();
                    System.out.print("在此换乘");
                    lineShow(idex22);
                    for (int j = n; j < idex2; j++) {
                        System.out.print(str[idex22][j] + "->");
                        count++;
                    }

                }
            }
            //start在换乘站的后面
            if (idex1 > m) {
                //end在换乘站的前面
                if (idex2 < n) {
                    lineShow(idex11);
                    for (int i = idex1; i > m; i--) {
                        System.out.print(str[idex11][i] + "->");
                        count++;
                    }
                    System.out.println();
                    System.out.print("在此换乘");
                    lineShow(idex22);
                    for (int j = n; j > idex2; j--) {
                        System.out.print(str[idex22][j] + "->");
                        count++;
                    }

                } else {
                    //end在换乘站后面
                    lineShow(idex11);
                    for (int i = idex1; i > m; i--) {
                        System.out.print(str[idex11][i] + "->");
                        count++;
                    }
                    System.out.println();
                    System.out.print("在此换乘");
                    lineShow(idex22);
                    for (int j = n; j < idex2; j++) {
                        System.out.print(str[idex22][j] + "->");
                        count++;
                    }

                }
            }

        }
        System.out.println(str[idex22][idex2]);
        //总共几站地
        System.out.println("共" + (count) + "站");
        return count;


    }


}